package controllers

import (
	"io"
	"mime/multipart"
	"os"
	"qmp_go_push/pkg/request"
	"qmp_go_push/pkg/response"
)

// Callable Http事件回调
type Callable func(requestParam *request.Request) response.Response

type ExampleParam struct {
	Name string `json:"name" form:"name" binding:"required"`
}
type ExampleResp struct {
	Name string `json:"name" form:"name"`
}

// Example 示例
func Example(requestParam *request.Request) response.Response {
	// 参数校验
	var param ExampleParam
	if err := requestParam.ShouldBind(&param); err != nil {
		return response.ResponseParamMiss()
	}
	// 参数封装
	// service调用
	resp, bizCode := param.Do()
	// 结构化数据返回
	return response.Response{Status: bizCode.Code, Message: bizCode.Message.Error(), Data: resp}
}

func (param ExampleParam) Do() (ExampleResp, response.CustomBizEnum) {
	return ExampleResp{Name: param.Name}, response.Success
}

func SaveUploadedFile(file *multipart.FileHeader, dst string) error {
	src, err := file.Open()
	if err != nil {
		return err
	}
	defer src.Close()
	//创建 dst 文件
	out, err := os.Create(dst)
	if err != nil {
		return err
	}
	defer out.Close()
	// 拷贝文件
	_, err = io.Copy(out, src)
	return err
}
